package com.zzxx.wechat.adpater;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.zzxx.wechat.common.vo.*;
import com.zzxx.wechat.entity.ArcteryxLink;
import com.zzxx.wechat.entity.ArcteryxProduct;
import com.zzxx.wechat.entity.ArcteryxSku;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


public class ArcteryxTaskAdapter {

    public static ArcteryxLinkVo arcteryxLink2Vo(ArcteryxLink arcteryxLink) {

        ArcteryxLinkVo arcteryxLinkVo = new ArcteryxLinkVo();
        BeanUtils.copyProperties(arcteryxLink, arcteryxLinkVo);
        arcteryxLinkVo.setExistLink(arcteryxLink.getUrl());
        arcteryxLinkVo.setDateTime(DateUtil.formatDateTime(new Date()));
        return arcteryxLinkVo;
    }


    public static ArcteryxLinkVo arcteryxLink2Vo(ArcteryxLink arcteryxLink, ArcteryxSpuInfoVo arcteryxSpuInfoVo) {

        ArcteryxLinkVo arcteryxLinkVo = new ArcteryxLinkVo();
        BeanUtils.copyProperties(arcteryxLink, arcteryxLinkVo);
        arcteryxLinkVo.setExistLink(arcteryxLink.getUrl());
        arcteryxLinkVo.setDateTime(DateUtil.formatDateTime(new Date()));

        arcteryxLinkVo.setSpuId(arcteryxSpuInfoVo.getSpuId());
        arcteryxLinkVo.setAnalyticsName(arcteryxSpuInfoVo.getAnalyticsName());

        List<ArcteryxSkuVo> arcteryxSkuVoList = arcteryxSpuInfoVo.getArcteryxSkuVoList();

        if (CollectionUtil.isEmpty(arcteryxSkuVoList)) {
            return arcteryxLinkVo;
        }

        Map<String, List<ArcteryxSkuVo>> arcteryxSkuMap = arcteryxSkuVoList.stream().collect(Collectors.groupingBy(ArcteryxSkuVo::getColourId));

        List<ArcteryxColourVo> arcteryxColourVoList = new ArrayList<>();
        for (Map.Entry<String, List<ArcteryxSkuVo>> entry : arcteryxSkuMap.entrySet()) {
            List<ArcteryxSkuVo> arcteryxSkuVos = entry.getValue();
            ArcteryxColourVo arcteryxColourVo = ArcteryxSkuAdapter.arcteryxSkuVo2ColourVo(arcteryxSkuVos);
            arcteryxColourVoList.add(arcteryxColourVo);
        }
        arcteryxLinkVo.setArcteryxColourVoList(arcteryxColourVoList);

        return arcteryxLinkVo;
    }


    public static void filterCommodityColor(List<ArcteryxProduct> arcteryxProductList, List<ArcteryxCommodityVo> arcteryxCommodityVos) {
        Map<String, ArcteryxProduct> arcteryxProductMap = arcteryxProductList.stream().collect(Collectors.toMap(ArcteryxProduct::getProductNo, v -> v));

        for (ArcteryxCommodityVo arcteryxCommodityVo : arcteryxCommodityVos) {
            ArcteryxProduct arcteryxProduct = arcteryxProductMap.get(arcteryxCommodityVo.getProductNo());

            if (StringUtils.isEmpty(arcteryxProduct.getColor())) {
                continue;
            }
            if (CollectionUtil.isEmpty(arcteryxCommodityVo.getArcteryxLinkVos())) {
                continue;
            }
            List<ArcteryxLinkVo> arcteryxLinkVos = arcteryxCommodityVo.getArcteryxLinkVos();

            if (CollectionUtil.isEmpty(arcteryxLinkVos)) {
                continue;
            }
            for (ArcteryxLinkVo arcteryxLinkVo : arcteryxLinkVos) {

                List<ArcteryxColourVo> arcteryxColourVoList = arcteryxLinkVo.getArcteryxColourVoList();
                if (CollectionUtil.isEmpty(arcteryxColourVoList)) {
                    continue;
                }
                List<ArcteryxColourVo> collect = arcteryxColourVoList.stream()
                        .filter(x -> arcteryxProduct.getColor().equalsIgnoreCase(x.getColourLabel()))
                        .collect(Collectors.toList());

                arcteryxLinkVo.setArcteryxColourVoList(collect);
            }
        }
    }
}
